package com.itheima.d07_net_udp_manytomany;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Objects;
import java.util.Scanner;

/**
 * 目标：了解UDP网络编程中的客户端
 */
public class Client {

    public static void main(String[] args) throws Exception {
        //1.创建发送数据的客户端对象（仍韭菜的人）
        DatagramSocket client = new DatagramSocket(7777);


        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("请say:");
            String msg = sc.next();


            //2.创建封装数据的数据包对象（装韭菜的盘子）
            byte[] sendMsg = msg.getBytes(); //发送的数据
            int sendMsgLength = sendMsg.length;   //发送数据的长度
            InetAddress ip = InetAddress.getLocalHost(); //发送数据的ip
            int port = 10086; //发送数据的端口

            DatagramPacket packet = new DatagramPacket(sendMsg,sendMsgLength,ip,port);

            //3.发送数据（仍韭菜）
            client.send( packet );
            System.out.println("数据已发送！");

            if(Objects.equals("886",msg)){
                System.out.println("byebye");

                break; //跳出循环
            }

        }
        //4.资源释放
        client.close();
    }
}
